﻿using System.Web.Http;
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Xml;
using In.Api.Dto.LisWs;
using Newtonsoft.Json;
using In.Api.Helpers;

namespace In.Api.Controllers
{
    public class LisWsController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(LisWsController));

        [HttpPost]
        [ActionName("GetExamReports")]
        public Response4GetExamReports GetExamReports(Request4GetExamReports request)
        {
            var wsClient = WsClientFactory.GetReportServiceClient();

            var xmlDoc = new XmlDocument();
            XmlNode root = xmlDoc.CreateElement("req");

            XmlNode pdfNameNode = xmlDoc.CreateElement("pdfName");
            pdfNameNode.InnerText = request.PdfReportName;
            root.AppendChild(pdfNameNode);
            xmlDoc.AppendChild(root);

            var requestXml = xmlDoc.InnerXml;
            var responseXml = "";
            List<string> pngNames;

            try
            {
                pngNames = wsClient.GetReportsByName(request.PdfReportName);
                wsClient.Close();
            }
            catch (Exception ex)
            {
                wsClient.Abort();
                Logger.ErrorFormat("调用 GetExamReports 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            var responseObj = new Response4GetExamReports();
            responseObj.PngReports = new List<string>();

            if (pngNames == null || pngNames != null && pngNames.Count == 0)
            {
                responseObj.ResultCode = "1";
                responseObj.ResultDesc = "获取图片报告失败！";
                responseXml = "<res><resultCode>1<resultCode><resultDesc>获取图片报告失败！</resultDesc></res>";
            }
            else
            {
                responseObj.ResultCode = "0";
                responseObj.ResultDesc = "获取图片报告成功！";
                foreach (var report in pngNames)
                {
                    responseObj.PngReports.Add(report);
                }
                responseXml = "<res><resultCode>0<resultCode><resultDesc>获取图片报告成功！</resultDesc></res>";
            }

            HospWsLogHelper.Log("GetExamReports", requestXml, responseXml);

            return responseObj;
        }


    }
}